package com.example.littleSpider.myapplication.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.base.BaseActivity;
import com.example.littleSpider.myapplication.myInterfaces.MethodInTitle;
import com.example.littleSpider.myapplication.view.TitleView;
import com.littlespider.mylibrary.util.T;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商品详情的页面
 * Created by littleSpider on 2017/12/13.
 */

public class GoodsDetailsActivity extends BaseActivity implements MethodInTitle{

    @BindView(R.id.title_view)
    TitleView mTitleView;
    @BindView(R.id.banner_view)
    MZBannerView mzBannerView;
    @BindView(R.id.web_view)
    WebView mWebView;
    @BindView(R.id.tv_count)
    TextView tv_count;
    @BindView(R.id.ll_content)
    LinearLayout ll_content;
    @BindView(R.id.ll_bottom_buttons)
    LinearLayout ll_bottom;
    @BindView(R.id.flow_layout)
    TagFlowLayout flowLayout;
    @BindView(R.id.tv_old_price)
    TextView tv_old_price;
    TagFlowLayout colorFlow;

    List<Integer> images = new ArrayList();
    List<String> tags = new ArrayList<>();

    String[] textContent = {"你是", "我的", "小呀", "小苹果", "怎么", "爱你", "都不", "嫌多", "春天", "又来到了", "花", "开满山坡", "种下", "希望", "就会收获"};
    int num = 1;
    private PopupWindow popupWindow;
    private PopupWindow infoPopupWindow;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_goods_detail;
    }

    @Override
    protected void initView() {
        num = Integer.parseInt(tv_count.getText().toString());
        mTitleView.initMyTitle("商品详情", this);
        mTitleView.hideTitleTextImage();
        images.add(R.drawable.spiderman_one);
        images.add(R.drawable.spiderman_two);
        images.add(R.drawable.spiderman_three);
        mzBannerView.setPages(images, new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new MyPageAdapter();
            }
        });
        mWebView.loadUrl("https://www.sanwen.net/subject/407513/");
        mWebView.setWebViewClient(new WebViewClient());
        for (int i = 0; i < textContent.length; i++) {
            tags.add(textContent[i]);
        }

        String price = "¥3180.00";
        SpannableString spannableString = new SpannableString(price);
        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
        spannableString.setSpan(strikethroughSpan, 0, price.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_old_price.setText(spannableString);
        flowLayout.setAdapter(new TagAdapter<String>(tags) {
            @Override
            public View getView(FlowLayout flowLayout, int i, String s) {
//                TextView tv = new TextView(GoodsDetailsActivity.this);
                TextView tv = (TextView) LayoutInflater.from(GoodsDetailsActivity.this).inflate(R.layout.flow_item, null);
                tv.setBackgroundResource(R.drawable.selector_goods_color);

                tv.setText(s);
                return tv;
            }

            @Override
            public void onSelected(int position, View view) {
                super.onSelected(position, view);
                TextView textView = (TextView) view;
                textView.setTextColor(Color.parseColor("#ffffff"));
//                textView.setBackgroundResource(R.drawable.product_item_select_back);
            }

            @Override
            public void unSelected(int position, View view) {
                super.unSelected(position, view);
                TextView textView = (TextView) view;
                textView.setTextColor(Color.parseColor("#A9A9A9"));
//                textView.setBackgroundResource(R.drawable.product_item_
//                        back);
            }
        });

    }

    @Override
    protected void initData() {

    }

    @Override
    public void setGoBack() {
        finish();
    }

    @Override
    public void onRightOne() {
        Intent intent = new Intent(GoodsDetailsActivity.this, MyEspeciallySaleActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRightTwo() {
        Intent intent = new Intent(GoodsDetailsActivity.this, ShoppingCartActivity.class);
        startActivity(intent);
    }

    @Override
    public void onTitleText() {

    }

    @OnClick(R.id.btn_subtract)
    public void subtract(){
        if (num > 0){
            num -= 1;
            tv_count.setText(String.valueOf(num));
        }
    }

    @OnClick(R.id.btn_add)
    public void add(){
        num += 1;
        tv_count.setText(String.valueOf(num));
    }

    @OnClick({R.id.rl_choose_type, R.id.rl_goods_param})
    public void showInfo(View view){
        int showType = 0;
        switch (view.getId()){
            case R.id.rl_choose_type: //选择规格
                showType = 0;
                break;
            case R.id.rl_goods_param: //产品参数
                showType = 1;
                break;
            default:
                break;
        }
        infoPopupWindow = new PopupWindow(GoodsDetailsActivity.this);
        View showView = null;
        if (showType == 0){
            showView = LayoutInflater.from(GoodsDetailsActivity.this).inflate(R.layout.popup_choose_type, null);
            showView.findViewById(R.id.iv_close).setOnClickListener(chooseClick);
            colorFlow = (TagFlowLayout) showView.findViewById(R.id.flow_layout);
            colorFlow.setAdapter(new TagAdapter<String>(tags) {
                @Override
                public View getView(FlowLayout flowLayout, int i, String s) {
                    TextView tv = (TextView) LayoutInflater.from(GoodsDetailsActivity.this).inflate(R.layout.flow_item, null);
                    tv.setBackgroundResource(R.drawable.selector_goods_color);
                    tv.setText(s);
                    return tv;
                }
            });
        }else if (showType == 1){
            showView = LayoutInflater.from(GoodsDetailsActivity.this).inflate(R.layout.popup_goods_info, null);
            showView.findViewById(R.id.tv_complete).setOnClickListener(chooseClick);
            LinearLayout linearLayout = (LinearLayout) showView.findViewById(R.id.ll_params);
            for (int i = 0; i < 8; i++) {
                View infoItem = View.inflate(GoodsDetailsActivity.this, R.layout.item_property, null);
                linearLayout.addView(infoItem);
            }
        }else {

        }
        showView.findViewById(R.id.background).setOnClickListener(chooseClick);
        infoPopupWindow.setContentView(showView);
        infoPopupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        infoPopupWindow.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        infoPopupWindow.setFocusable(true);
        infoPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#66334455")));
        infoPopupWindow.showAtLocation(ll_bottom, 0, 0, 0);
    }

    /**
     * 底部四个按钮的点击事件
     * @param view
     */
    @OnClick({R.id.rl_contact, R.id.rl_share, R.id.rl_join, R.id.rl_buying})
    public void bottomButtons(View view){
        switch (view.getId()){
            case R.id.rl_contact: //联系商家
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("你确定要给商家打电话吗?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:18368024463"));
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        })
                        .show();
                break;
            case R.id.rl_share: //分享
                View popupView = LayoutInflater.from(GoodsDetailsActivity.this).inflate(R.layout.popup_share, null);
                popupView.findViewById(R.id.ll_wechat_friend);
                popupView.findViewById(R.id.ll_friends).setOnClickListener(popupClick);
                popupView.findViewById(R.id.ll_wechat_friend).setOnClickListener(popupClick);
                popupView.findViewById(R.id.tv_cancel).setOnClickListener(popupClick);
                popupView.findViewById(R.id.background).setOnClickListener(popupClick);
                popupWindow = new PopupWindow(this);
                popupWindow.setContentView(popupView);
                popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
                popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setBackgroundDrawable(new ColorDrawable(0x3300ff99));
//                popupWindow.setBackgroundDrawable(new ColorDrawable(0x00ff99));
//                popupView.setBackgroundColor(Color.parseColor("#FFFFFF"));
//                popupWindow.setBackgroundDrawable(new ColorDrawable(0x33888888));
                popupWindow.setFocusable(true);
//                popupWindow.showAtLocation(ll_bottom, 0, 0, 0);
                popupWindow.showAtLocation(ll_bottom, 0, 0, 100);
                popupWindow.showAsDropDown(mTitleView);
                break;
            case R.id.rl_join: //加入购物车
                break;
            case R.id.rl_buying: //立即购买
                Intent intent1 = new Intent(GoodsDetailsActivity.this, GoodsOrderActivity.class);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }

    /**
     * 选择规格.数量的弹窗里面的控件点击事件
     */
    View.OnClickListener chooseClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
//                case R.id.background:
//                    break;
//                case R.id.iv_close:
//                    break;
                default:
                    infoPopupWindow.dismiss();
                    break;
            }
        }
    };

    /**
     * 分享弹窗里面的控件点击事件
     */
    View.OnClickListener popupClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.ll_wechat_friend:
                    T.show(GoodsDetailsActivity.this, "我要分享给朋友");
                    break;
                case R.id.ll_friends:
                    T.show(GoodsDetailsActivity.this, "我要分享到朋友圈");
                    break;
                default:
                    break;
            }
            popupWindow.dismiss();
        }
    };

    class MyPageAdapter implements MZViewHolder<Integer> {

        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(GoodsDetailsActivity.this).inflate(R.layout.view_banner, null);
            mImageView = (ImageView) view.findViewById(R.id.image);
            return view;
        }

        @Override
        public void onBind(Context context, int i, Integer integer) {
            mImageView.setImageResource(integer);
        }
    }
}
